package com.example.serviceorder.controller;

import com.example.order.Order;
import com.example.serviceorder.feign.ProductFeignClient;
import com.example.serviceorder.properties.OrderProperties;
import com.example.serviceorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RefreshScope//自动刷新
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 根据订单ID查询订单（模拟数据）
     */
    @GetMapping("/{id}")
    public Order getOrderById(@PathVariable("id") Long id) {
        return orderService.getOrderById(id);
    }

    @Value("${order.timeout}")
    String orderTimeout;
    @Value("${order.auto-confirm}")
    String orderAutoConfirm;

    @GetMapping("/getConfigByValueAnno")
    public String getConfigByValueAnno(){
        return "order.timeout="+orderTimeout+"； " +
                "order.auto-confirm="+orderAutoConfirm +"；";
    }

    @Autowired
    OrderProperties orderProperties;


    @GetMapping("/getConfigByConfigurationProperties")
    public String getConfigByConfigurationProperties(){
        return "order.timeout="+orderProperties.getTimeout()+"； " +
                "order.auto-confirm="+orderProperties.getAutoConfirm() +"；"+
                "order.service-name="+orderProperties.getServiceName() +"；"+
                "order.namespace="+orderProperties.getNamespace() +"；";
    }

    /**
     * 使用OpenFeign查询微服务
     */
    @GetMapping("/getOrderFromOpenFeignById/{id}")
    public Order getOrderFromOpenFeignById(@PathVariable("id") Long id) {
        return orderService.getOrderFromOpenFeignById(id);
    }
    /**
     * 使用OpenFeign查询微服务
     */
    @GetMapping("/getProductsFromOpenFeignByIdSecure/{id}")
    public Order getProductsFromOpenFeignByIdSecure(@PathVariable("id") Long id) {
        return orderService.getProductsFromOpenFeignByIdSecure(id);
    }

}
